import { makeAutoObservable, toJS } from "mobx";
import { v4 as uuidv4 } from 'uuid';


export enum localStorageKey {
    Gategory = "gategory",
    Task = "task",

}

export interface IDragTaskProps {
    newCategoryKey: string;
    isFinish: boolean;
    newCategoryIndex: number;
    originalCategoryKey: string;
    originalCategoryIndex: number;
    currentTaskName: string;
    cureentTaskIndex: number;
}


export interface ITaskCategory {
    taskCategoryName: string;
    taskCategoryKey: string;
    taskCount: Number;
}

export interface ITask {
    taskName: string;
    taskIndex: number;
    isFinish: boolean;
}



const taskJsonData = localStorage.getItem(localStorageKey.Gategory);

const taskData: ITaskCategory[] = JSON.parse(taskJsonData ? taskJsonData : JSON.stringify([])) as ITaskCategory[];

export class TaskStore {

    // store数据
    taskCategory: ITaskCategory[] = taskData

    currentTasks: ITask[] = [];


    // finishTasks: ITask[] = [];

    constructor() {
        //  自定绑定
        makeAutoObservable(this);
    }

    /**
     * addTaskCategory
     * @param name 
     */
    addTaskCategory = (name: string) => {
        const newTaskCategory: ITaskCategory = {
            taskCategoryName: name,
            taskCategoryKey: uuidv4(),
            taskCount: 0
        }
        this.taskCategory.push(newTaskCategory)
        localStorage.setItem(localStorageKey.Gategory, JSON.stringify(this.taskCategory))

    }

    displayTasks = (taskCategoryKey: string) => {
        const taskKey = localStorageKey.Task + ":" + taskCategoryKey;

        const tasksJsonData = localStorage.getItem(taskKey);

        const localTasks = JSON.parse(tasksJsonData ? tasksJsonData : JSON.stringify([])) as ITask[];

        this.currentTasks = localTasks;

        console.log(localTasks)

    }

    dragTaskToCategory = (dragTask: IDragTaskProps) => {

        this.addTask(dragTask.newCategoryKey, dragTask.currentTaskName, dragTask.newCategoryIndex, dragTask.isFinish)

        this.removeTask(dragTask.originalCategoryKey, dragTask.currentTaskName, dragTask.cureentTaskIndex, dragTask.originalCategoryIndex)


    }

    removeTask = (taskCategoryKey: string, taskName: string, taskIndex: number, taskCategoryIndex: number) => {

        const taskKey = localStorageKey.Task + ":" + taskCategoryKey;

        const tasksJsonData = localStorage.getItem(taskKey);

        const localTasks = JSON.parse(tasksJsonData ? tasksJsonData : JSON.stringify([])) as ITask[];

        localTasks.splice(taskIndex, 1)

        this.currentTasks = localTasks;


        localStorage.setItem(taskKey, JSON.stringify(localTasks))

        this.taskCategory[taskCategoryIndex].taskCount = localTasks.length;
        localStorage.setItem(localStorageKey.Gategory, JSON.stringify(this.taskCategory))

    }


    recoveryTask = (taskCategoryKey: string, currentTaskIndex: number) => {


        const taskKey = localStorageKey.Task + ":" + taskCategoryKey;

        const tasksJsonData = localStorage.getItem(taskKey);

        const localTasks = JSON.parse(tasksJsonData ? tasksJsonData : JSON.stringify([])) as ITask[];

        localTasks[currentTaskIndex].isFinish = false;

        this.currentTasks = localTasks;


        localStorage.setItem(taskKey, JSON.stringify(localTasks))
    }

    finishTask = (taskCategoryKey: string, currentTaskIndex: number) => {


        const taskKey = localStorageKey.Task + ":" + taskCategoryKey;

        const tasksJsonData = localStorage.getItem(taskKey);

        const localTasks = JSON.parse(tasksJsonData ? tasksJsonData : JSON.stringify([])) as ITask[];

        localTasks[currentTaskIndex].isFinish = true;

        this.currentTasks = localTasks;


        localStorage.setItem(taskKey, JSON.stringify(localTasks))
    }

    /**
     * add Task in local
     * @param taskCategoryKey 
     * @param taskName 
     */
    addTask = (taskCategoryKey: string, taskName: string, taskCategoryIndex: number,isFinish:boolean) => {

        const taskKey = localStorageKey.Task + ":" + taskCategoryKey;

        const tasksJsonData = localStorage.getItem(taskKey);

        const localTasks = JSON.parse(tasksJsonData ? tasksJsonData : JSON.stringify([])) as ITask[];

        localTasks.push({ "taskName": taskName, isFinish: isFinish, taskIndex: localTasks.length });


        this.currentTasks = localTasks;


        localStorage.setItem(taskKey, JSON.stringify(localTasks))

        this.taskCategory[taskCategoryIndex].taskCount = localTasks.length;
        localStorage.setItem(localStorageKey.Gategory, JSON.stringify(this.taskCategory))


    }
}

